Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

2.2K
Views
SQLSTATE[HY000] [2002] Connection refused. Laravel API hosted on Docker with WSL2 Ubuntu 18.04

I am trying to create a new item via postman to my laravel app ran in docker. The docker containers are auto-generated via ddev which provides a development environment and ran using WSL2 Ubuntu 18.04. So here is the catch, I can "php artisan migrate" just fine I can even "php artisan tinker" and create one just fine but when sending request via postman from my host machine I get this error.

Illuminate\Database\QueryException: SQLSTATE[HY000] [2002] Connection refused (SQL: select count(*) as aggregate from users where id = 1) in file /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 671

I had solved this issue yesterday with this https://www.craigforrester.com/posts/windows-subsystem-for-linux-disable-ipv6-for-apt/ but tried it when I got this error again today and no luck.

Things I've Tried:

  • Setting DB_HOST to 127.0.0.1, wsl_ip, docker_gateway_ip, docker_mysql_ip, localhost, mysql. And actually I get the same error on all of them except localhost, which makes sense.
  • Forcing ipv4 https://www.craigforrester.com/posts/windows-subsystem-for-linux-disable-ipv6-for-apt/
  • Changing gai.conf settings https://www.reddit.com/r/bashonubuntuonwindows/comments/7u1le5/disable_ipv6_or_prefer_ipv4_first/
  • Changing grub config https://askubuntu.com/questions/1046057/disabling-ipv6-in-ubuntu-server-18-04

What I Think It Is: I am 99% certain this is not a credentials issue and more so a routing issue, cause like I said in the first paragraph I can migrate and create from the WSL Ubuntu. I think the routing issue is either from my host to the WSL or from WSL to mysql. But then again my request seems to be going through otherwise I wouldve got some kind of PHP connection error and not a MySQL connection error

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I had this issue a while back it turned out that I had to set DB_HOST to the name of the docker mysql container:-

docker-compose.yml:-

version: '3.1'

services:
    php:
        build:
            context: .
            dockerfile: .docker/Dockerfile
        image: larastock
        ports:
            - 8000:80
        restart: always
        volumes:
            - .:/var/www/html
        networks:
            - larastock
    mysql:
        image: mysql:8.0
        volumes:
            - db_data:/var/lib/mysql
        restart: always
        ports:
            - 3306:3306
        environment:
            MYSQL_DATABASE: larastock
            MYSQL_USER: root
            MYSQL_PASSWORD: password
            MYSQL_ROOT_PASSWORD: password
        networks:
            - larastock
    phpmyadmin:
        depends_on:
            - mysql
        image: phpmyadmin/phpmyadmin
        restart: always
        ports:
            - 8001:80
        environment:
            PMA_HOST: mysql
            MYSQL_ROOT_PASSWORD: password 
        networks:
            - larastock
networks:
    larastock:
volumes:
    db_data:

.env:-

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=larastock
DB_USERNAME=root
DB_PASSWORD=password
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!